﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Framework;
using System.Globalization;
using NLBS.Entity;

namespace NLBS.Data.Business
{
   public class UserData:CommonBase
    {
       public static User GetUserByID(int userID)
       {
          
           User user = GetPageUser().Find(delegate(User u)
            {
                return (u.UserID == userID);
            });
          if (user != null)
              return user;
          return null;
       }

       public static User GetUserByName(string name)
       {

           User user = GetPageUser().Find(delegate(User u)
           {
               return (u.UserName == name);
           });
           if (user != null)
               return user;
           return null;
       }

       private static readonly string UserPageKey = "UserList";
       public static List<User> GetPageUser()
       {
           ContentCache cache = ContentCache.Instantiate();
           List<User> lists = (List<User>)cache[UserPageKey];
           if (lists == null)
           {
               lists =new List<User>(DbProvider.Instance().GetUser());
               if (lists != null)
               {
                   cache.Insert(UserPageKey, lists, CacheDuration.Long);
               }
           }

           return lists;
       }

       public static void UpdateUser(User user)
       {
           DbProvider.Instance().UpdateUser(user);
       }

       public static void DeleteUser(int userID)
       {
           
           User user = GetUserByID(userID);
           if(GetPageUser().Contains(user))
           {
               GetPageUser().Remove(user);
           }
           DbProvider.Instance().DeleteUser(userID);
           BlogInfo.Instance().UserCount--;
       }

       public static void InsertUser(User user)
       {
           DbProvider.Instance().InsertUser(user);
           GetPageUser().Add(user);
           BlogInfo.Instance().UserCount++;
       }
    }
}
